/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.jessyan.lifecyclemodel.base;

import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public abstract class BaseFraction extends Fraction {

    public abstract int getUIContent();

    public abstract void initComponent();

    private final HiLogLabel lable = new HiLogLabel(HiLog.LOG_APP, 0x00201, "BaseFraction");

    /**
     * 进入 INACTIVE 状态后来到前台，然后系统调用此回调
     **/
    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component mComponent;
        // 将布局文件转换成组件对象
        mComponent = scatter.parse(getUIContent(), container, false);
        // 初始化组件
        initComponent();
        return mComponent;
    }

    /**
     * 初始化
     **/
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
    }

    /**
     * 应用与用户交互的状态
     **/
    @Override
    public void onActive() {
        super.onActive();

    }

    /**
     * 重新回到前台时，调用
     **/
    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    /**
     * 失去焦点时，系统将调用此回调
     **/
    @Override
    protected void onInactive() {
        super.onInactive();

    }

    /**
     * 系统将进行销毁，触发此回调函数
     **/
    @Override
    protected void onStop() {
        super.onStop();
    }

    /**
     * 获取基于此Element的字符串值
     **/
    public String getString(int resId) {
        try {
            return getFractionAbility().getResourceManager().getElement(resId).getString();
        } catch (Exception e) {
//            e.printStackTrace();
            HiLog.error(lable,"BaseFraction", e);
        }
        return "";
    }

    /**
     * 获取基于此Element的颜色值
     **/
    public int getColor(int colorId) {
        try {
            return getFractionAbility().getResourceManager().getElement(colorId).getColor();
        } catch (Exception e) {
//            e.printStackTrace();
            HiLog.error(lable,"BaseFraction", e);
        }
        return 0;
    }

}
